package com.cjy.controller;

import com.cjy.pojo.Type;
import com.cjy.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TypesController {

    @Autowired
    private TypeService typeService;

    /**
     * 跳转页面--编辑分类页面
     * @return
     */
    @GetMapping("/types")
    public String types(Model model){
        model.addAttribute("type",new Type());
        return "admin/type-input";
    }

    /**
     * 博客分页管理
     * sort = {"id"} 根据id排序
     * direction = Sort.Direction.DESC 倒叙排序
     * @return
     */
    @GetMapping("/typeList")
    public String typeList(@PageableDefault(size = 8,sort = {"id"},direction = Sort.Direction.DESC)
                           Pageable pageable, Model model){
        Page<Type> types = typeService.listType(pageable);
        model.addAttribute("page",types);
        return "admin/typeManager";
    }

    /**
     * 校验并添加分类
     * @param type 分类对象
     * @param result 校验结果对象
     * @param attributes 参数设置
     * @return
     *
     * @Valid Type type , BindingResult result 这两个参数必须紧紧挨着，不然会报错，进而不能校验注解的配置的校验
     * Field error in object 'type' on field 'name': rejected value []; codes
     */
    @PostMapping("/addTypes")
    public String addType(@Valid Type type , BindingResult result, RedirectAttributes attributes){
        Type typeByName = typeService.getTypeByName(type.getName());
        if (typeByName!=null){
            result.rejectValue("name","nameError","该分类已存在，不能重复添加！");
        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/type-input";
        }
        Type t = typeService.saveType(type);
        if (t==null){
            // 保存失败
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/typeList";
    }

    @PostMapping("/types/{id}")
    public String editType(@Valid Type type , BindingResult result,@PathVariable Long id , RedirectAttributes attributes){
        Type typeByName = typeService.getTypeByName(type.getName());
        if (typeByName!=null){
            result.rejectValue("name","nameError","该分类已存在，不能重复添加！");
        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/type-input";
        }
        Type t = typeService.updateType(id,type);
        if (t==null){
            // 保存失败
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/typeList";
    }

    /**
     * 编辑连接
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}/input")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("type",typeService.getType(id));
        return "admin/type-input";
    }

    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        typeService.deleteType(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/typeList";
    }

}
